package love.bot.forli.group.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import love.bot.forli.group.review.bean.Dto.ResultDto;
import love.bot.forli.group.review.bean.Dto.TestDto;
import love.bot.forli.group.review.bean.Dto.TestViewDto;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.bean.Vo.CreateVo;
import love.bot.forli.group.review.bean.Vo.SubmitVo;
import love.bot.forli.group.review.entity.Test;
import love.bot.forli.group.review.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测试控制器
 *
 * @author Kontori
 */
@Api(tags = "测试相关接口")
@RestController
@RequestMapping("/test")
@Transactional(rollbackFor = {Throwable.class})
@Slf4j
public class TestController {

    private TestService testService;

    @Autowired
    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    @ApiOperation("创建随机试题")
    @PostMapping("/create")
    public Result<Object> create(@RequestBody CreateVo createVo) {
        return testService.createTest(createVo);
    }

    @ApiOperation("获取试题")
    @GetMapping("/{testId}")
    public Result<TestDto> get(@PathVariable(value = "testId") Integer testId) {
        return testService.getTest(testId);
    }

    @ApiOperation("提交试题")
    @PostMapping("/submit")
    public Result<ResultDto> submit(@RequestBody SubmitVo submitVo) {
        return testService.submitTest(submitVo);
    }

    @ApiOperation("获取试题答题情况")
    @GetMapping("/view/{testId}")
    public Result<TestViewDto> view(@PathVariable(value = "testId") Integer testId) {
        return testService.view(testId);
    }

    @ApiOperation("获取所有试题记录")
    @GetMapping("/list")
    public Result<List<Test>> list(@ApiParam(value = "页码") @RequestParam Integer page,
                                   @ApiParam(value = "数量") @RequestParam Integer limit,
                                   @ApiParam(value = "QQ号搜索") @RequestParam String qq) {
        return Result.success(testService.page(new Page<>(page, limit), new QueryWrapper<Test>()
                .like("tester_qq", qq)).getRecords());
    }

    @ApiOperation("删除试题记录")
    @DeleteMapping("/delete/{testId}")
    public Result<String> delete(@PathVariable("testId") Integer testId) {
        return testService.delete(testId);
    }

    @ApiOperation("获取试题总数")
    @GetMapping("/pageSize")
    public Result<Long> pageSize(@ApiParam(value = "QQ号搜索") @RequestParam String qq) {
        return Result.success(testService.count(new QueryWrapper<Test>()
                .like("tester_qq", qq)));
    }

}
